using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;


using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.BLL
{
    public class IpPart
    {


        private static readonly PlugNT.Cms.DAL.IpPart dal = new PlugNT.Cms.DAL.IpPart();








        public IpPartInfo GetIpPartInfo(int id)
        {
            return dal.GetIpPartInfo(id);

        }





        public bool HasCurrent()
        {
            string host = System.Web.HttpContext.Current.Request.UserHostAddress;
            string []arrHost=host.Split('.');
            if (arrHost.Length != 4)
            {
                return false;
            }
            double tmpHost = int.Parse(arrHost[0]) * 256 * 256 * 256
                + int.Parse(arrHost[1]) * 256 * 256
                + int.Parse(arrHost[2]) * 256
                + int.Parse(arrHost[3]);
            return dal.Exists(" and ip_start<=" + tmpHost + " and ip_end>=" + tmpHost);
        }








        public bool Add(string ipStart,string ipEnd)
        {
            IpPartInfo model = new IpPartInfo();
            string[] arrHost = ipStart.Split('.');
            double ip_start = int.Parse(arrHost[0]) * 256 * 256 * 256
                + int.Parse(arrHost[1]) * 256 * 256
                + int.Parse(arrHost[2]) * 256
                + int.Parse(arrHost[3]);
            model.ip_start = ip_start;
            model.ip_start_str = ipStart;

            arrHost = ipEnd.Split('.');
            double ip_end = int.Parse(arrHost[0]) * 256 * 256 * 256
                + int.Parse(arrHost[1]) * 256 * 256
                + int.Parse(arrHost[2]) * 256
                + int.Parse(arrHost[3]);
            model.ip_end = ip_end;
            model.ip_end_str = ipEnd;
            model.addtime = DateTime.Now;
            return dal.Add(model);
        }








        public bool Update(int id,string ipStart, string ipEnd)
        {
            IpPartInfo model = new IpPartInfo();
            string[] arrHost = ipStart.Split('.');
            double ip_start = int.Parse(arrHost[0]) * 256 * 256 * 256
                + int.Parse(arrHost[1]) * 256 * 256
                + int.Parse(arrHost[2]) * 256
                + int.Parse(arrHost[3]);
            model.ip_start = ip_start;
            model.ip_start_str = ipStart;

            arrHost = ipEnd.Split('.');
            double ip_end = int.Parse(arrHost[0]) * 256 * 256 * 256
                + int.Parse(arrHost[1]) * 256 * 256
                + int.Parse(arrHost[2]) * 256
                + int.Parse(arrHost[3]);
            model.ip_end = ip_end;
            model.ip_end_str = ipEnd;
            model.id = id;
            return dal.Update(model);
        }






        public bool Delete(int id)
        {
            return dal.Delete(id);
        }








        public DataTable GetList(string andWhere)
        {
            return dal.GetList(andWhere);
        }



    }
}

